﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using WmWebSystem.Models;
using WmWebSystem.Service;
using WmWebSystem.DbHelper;

namespace WmWebSystem.Controllers
{
    public class ItemController : Controller
    {
        WmDb mydb = new WmDb();
        //
        // GET: /Item/

        [Authorize]
        public ActionResult Create(string pno)
        {
            ViewBag.No = pno;
            return View();
        }


        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(ChildProject model)
        {
            //根据文件类型存储不同的子项目
            string pno = Request.Form["projectNo"];
            
            string url = "";
            string delPath = "";
            string extension = "";
            string fileOldName = "";

            foreach (string created in Request.Files)
            {
                if (!WmWebSystem.Service.HasFiles.HasFile(Request.Files[created])) continue;

                //string mimiType = Request.Files[created].ContentType.ToLower();
                fileOldName = Path.GetFileName(Request.Files[created].FileName);
                extension = System.IO.Path.GetExtension(fileOldName);//扩展名 ".xxxx"

                Stream fileStream = Request.Files[created].InputStream;

                //添加一个判断"ProjectContent"文件夹是否存在的方法
                string CreatePath = AppDomain.CurrentDomain.BaseDirectory + @"ProjectContent\" + pno;
                string SavePath = HasFolders.HasFolder(CreatePath);
                //文件重命名
                string fileNewName = RandomNumberTo.RandomNumberToName() + extension;

                //文件未重命名
                //fileOldName = Path.GetFileName(Request.Files[created].FileName);
                Request.Files[created].SaveAs(Path.Combine(SavePath, fileNewName));

                delPath = SavePath + fileNewName.ToString();
                url = "ProjectContent\\" + pno + "\\" + fileNewName;
            }

            ChildProjectHelper.CreateNewToChildProjects(pno,"", fileOldName, "", url, delPath, extension);

            return RedirectToAction("ProjectView", "Project", new { S = pno });

        }

        public Guid Swfupload(string pno)
        {

            //添加一个判断"ProjectContent"文件夹是否存在的方法
            string uploadsFolder = HasFolders.HasFolder(AppDomain.CurrentDomain.BaseDirectory + @"ProjectContent\" + pno);

            //生成不重复编码串
            Guid identifier = Guid.NewGuid();

            string oldname = Path.GetFileName(Request.Files["Filedata"].FileName);
            string ext = System.IO.Path.GetExtension(oldname).ToLower();
            string newName = identifier.ToString() + ext;

            var uploadsPath = Path.Combine(uploadsFolder, newName);
            var httpfile = Request.Files["Filedata"];

            string path = uploadsFolder + newName.ToString();
            string url = "~\\ProjectContent\\" + pno + "\\" + newName;


            if (httpfile != null)
            {
                httpfile.SaveAs(uploadsPath);
                ChildProjectHelper.CreateNewToChildProjects(pno, null, oldname, null, url, path, ext);
            }
            return identifier;
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            var item = mydb.ChildProjects.Find(id);
            return View(item);
        }
        [Authorize]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult DoEdit(ChildProject model)
        {
            //model.ChildName = HttpUtility.HtmlDecode(Request.Form["ChildName"]);
            //model.Content = HttpUtility.HtmlDecode(Request.Form["Content"]);
            try
            {
                var childProject = mydb.ChildProjects.Find(model.ChildProjectId);
                UpdateModel(childProject);
                mydb.SaveChanges();
                return RedirectToAction("Details", "Item", new { id = model.ChildProjectId });
            }
            catch (Exception e)
            {
                string error = "修改失败，请查看详细错误信息:" + e;
                return RedirectToAction("Error", "Notice", new { message = error });
            }
        }

        [Authorize]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var childProject = mydb.ChildProjects.Find(id);
            return View(childProject);
        }

        //[Authorize]
        public ActionResult DoDelete(int id)
        {
            var project = mydb.ChildProjects.Find(id);
            ChildProjectHelper.DeleteFromChildProjects(id);
            return RedirectToAction("Details","Project", new { id = id });
        }

        public ActionResult Details(int id) 
        {
            var item = mydb.ChildProjects.Find(id);
            return View(item);
        }


    }
}
